﻿
namespace SistemaLogistica.Windows.Forms.BienPatrimonial.Mantenedores
{
    using System;
    using System.Windows.Forms;
    using EDOSwit.Controls.ComboBox;
    using EDOSwit.Controls.Helpers;
    using SistemaLogistica.Business.Entities.Types;
    using SistemaLogistica.Business.Logic;
    using SistemaLogistica.Windows.Helpers;

    public partial class FrmBienes : Form, IForm
    {
        public int TipoBien
        {
            get { return this.cmbTipo.SelectedIndex; }
            set { this.cmbTipo.SelectedIndex = value; }
        }

        public bool BloquearTipoBien
        {
            get { return this.cmbTipo.Enabled; }
            set { this.cmbTipo.Enabled = value; }
        }

        public FrmBienes()
        {
            InitializeComponent();
            this.cmbTipo.Items.AddRange(Enum.GetNames(typeof(BienType)));
        }

        private void FrmBienes_Load(object sender, EventArgs e)
        {
            try
            {
                this.bienBindingSource.DataSource = BienManager.Instancia.Listar();
                this.bienBindingNavigator.BindingSource = this.bienBindingSource;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, Constantes.TituloMensaje, ex.Message);
            }
        }

        private void FrmBienes_Deactivate(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void FrmBienes_Leave(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void FrmBienes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                this.Hide();
            }
        }

        private void dgvBienes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape || e.KeyData == Keys.Enter)
            {
                dgvBienes_CellDoubleClick(sender, new DataGridViewCellEventArgs(0, this.bienBindingSource.Position));
            }
        }

        private void dgvBienes_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.bienBindingSource.Current != null)
            {
                Identificador = this.bienBindingSource.Current;
                OnAfterRowSelect(sender, Identificador);
            }
            this.Hide();
        }

        private void txtBuscar_TextChanged(object sender, EventArgs e)
        {
            Buscar();
        }

        private void cmbTipo_SelectedIndexChanged(object sender, EventArgs e)
        {
            Buscar();
        }

        private void Buscar()
        {
            try
            {
                switch (this.cmbTipo.SelectedIndex)
                {
                    case 0: // Bien
                        this.bienBindingSource.DataSource = BienManager.Instancia.Buscar(this.txtBuscar.Text);
                        break;
                    case 1: // Vehiculo
                        this.bienBindingSource.DataSource = VehiculoManager.Instancia.Buscar(this.txtBuscar.Text);
                        break;
                    case 2: // Telefono
                        this.bienBindingSource.DataSource = TelefonoManager.Instancia.Buscar(this.txtBuscar.Text);
                        break;
                    case 3: // Patrimonial
                        this.bienBindingSource.DataSource = BienPatrimonialManager.Instancia.Buscar(this.txtBuscar.Text);
                        break;
                }
                this.bienBindingNavigator.BindingSource = this.bienBindingSource;
            }
            catch (Exception ex)
            {
                Mensaje.ShowMessageAlert(this.ParentForm, Constantes.TituloMensaje, ex.Message);
            }
        }

        #region Miembros de IForm

        public event AfterRowSelectEventHandler AfterRowSelectEvent;

        public object Identificador { get; set; }

        public void OnAfterRowSelect(object sender, object identity)
        {
            if (AfterRowSelectEvent != null)
            {
                AfterRowSelectEvent(sender, identity);
            }
        }

        #endregion
    }
}
